package com.fowo.api.controller.purchase;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.*;
import com.fowo.api.flow.service.CallBackApprovalResults;
import com.fowo.api.model.inventory.list.InventoryListSubmitAction;
import com.fowo.api.model.purchase.change.order.PurchaseChangeOrderBulkAuditingAction;
import com.fowo.api.service.AdjustmentOrderService;
import com.fowo.api.service.InventoryListService;
import com.fowo.api.service.handleWork.InventoryListSubmit;
import com.fowo.api.service.impl.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class CallBackApprovalResultsImpl implements CallBackApprovalResults {
    @Resource
    private PurchaseOrderServiceImpl purchaseOrderService;
    @Resource
    private PurchasePlansServiceImpl purchasePlansService;
    @Resource
    private PurchasePlansProductServiceImpl purchasePlansProductService;
    @Resource
    private FundsPayOrderServiceImpl fundsPayOrderService;
    @Resource
    private InventoryListServiceImpl inventoryListService;
    @Resource
    private InventoryListSubmitAction inventoryListSubmitAction;
    @Resource
    private AdjustmentOrderServiceImpl adjustmentOrderService;
    @Resource
    private PurchaseChangeOrderServiceImpl PurchaseChangeOrderService;
    @Autowired(required = false)
    protected PurchaseChangeOrderBulkAuditingAction purchaseChangeOrderBulkAuditingAction;

    @Override
    @Transactional(rollbackFor = Exception.class,readOnly = true)
    public void updateRecordStatus(String formName, Long recordId, Integer wfStatus,List<HistoricActivityInstance> flowList) {
        if(formName.equals("PurchaseOrder")){
            PurchaseOrder byId = purchaseOrderService.getById(recordId);

            if(wfStatus==null){
                byId.setAnStatus("121");//设置为待审批状态

            }else{
                //流程走完了回调
                if(wfStatus.intValue()==3){
                    byId.setAnStatus("-1");//设置为作废状态
                }

                //结束
                else if(wfStatus.intValue()==4){
                    byId.setAnStatus("1");//设置为待下单状态
                }
            }
            purchaseOrderService.updateById(byId);
        }else if(formName.equals("PurchaseChangeOrder")){
            PurchaseChangeOrder byId = PurchaseChangeOrderService.getById(recordId);

            if(wfStatus==null){
                byId.setStatus("0");//设置为待审批状态
                PurchaseChangeOrderService.updateById(byId);
            }else{
                //流程走完了回调
                if(wfStatus.intValue()==3){
                    byId.setStatus("-1");//设置为作废状态
                    PurchaseChangeOrderService.updateById(byId);
                }
                //结束
                else if(wfStatus.intValue()==4){
                    List<Long> idList=new ArrayList<>();
                    idList.add(Long.valueOf(recordId));
                    try {
                        purchaseChangeOrderBulkAuditingAction.run(idList);
//                        byId.setStatus("1");//设置为待下单状态
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        }else if(formName.equals("PurchasePlans")){
            PurchasePlans byId = purchasePlansService.getById(recordId);
            List<PurchasePlansProduct> list = purchasePlansProductService.list(Wrappers.<PurchasePlansProduct>query().lambda().eq(PurchasePlansProduct::getParentPurchasePlans, byId.getId()));
            if(wfStatus==null){
                byId.setStatus("1");
                list.forEach(x->x.setStatus("1"));
            }else{
                //流程走完了回调
                if(wfStatus.intValue()==3){
                    byId.setStatus("4");//设置为驳回状态
                    list.forEach(x->x.setStatus("4"));
                }
                if(wfStatus.intValue()==2){
                    byId.setStatus("4");//设置为作废状态
                }
                //结束
                else if(wfStatus.intValue()==4){
                    byId.setStatus("2");//设置为采购状态
                    list.forEach(x->x.setStatus("2"));
                }
            }
            purchasePlansService.updateById(byId);
            list.forEach(x->purchasePlansProductService.updateById(x));
        }else if(formName.equals("FundsPayOrder")){
            FundsPayOrder fundsPayOrder = fundsPayOrderService.getById(recordId);
            if(wfStatus==null){
                fundsPayOrder.setStatus("QK01");
            }else{
                //流程走完了回调
                if(wfStatus.intValue()==3){
                    fundsPayOrder.setStatus("QK04");//设置为驳回状态
                }
                //结束
                else if(wfStatus.intValue()==4){
                    fundsPayOrder.setStatus("QK02");//设置为付款状态
                }
            }
            fundsPayOrderService.updateById(fundsPayOrder);
        }else if(formName.equals("InventoryList")){ // 盘点单
            InventoryList inventoryList = inventoryListService.getById(recordId);
            if(wfStatus==null){
                inventoryList.setStatus("5");
            }else{ //流程走完了回调
                //拒绝
                if(wfStatus.intValue()==3){
                    inventoryList.setStatus("4");//盘点作废
                    try {
                        inventoryListService.cancellation(recordId);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                //同意
                else if(wfStatus.intValue()==4){
                    inventoryList.setStatus("3");//盘点结束
                    try {
                        inventoryListSubmitAction.run(recordId);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            inventoryListService.updateById(inventoryList);
        }else if(formName.equals("AdjustmentOrder")){ //调整单
            AdjustmentOrder byId = adjustmentOrderService.getById(recordId);
            if(wfStatus==null){
                byId.setStatus("40");
                if(byId.getType().equals("1")){
                    //锁库存
                    try{
                        adjustmentOrderService.lockpInventory(byId.getId());
                        adjustmentOrderService.locksInventory(byId.getId());
                    }catch (Exception e){
                        Assert.isTrue(false,e.getLocalizedMessage());
                    }
                }
            }else{
                //拒绝
                if(wfStatus.intValue()==3){
                    byId.setStatus("5");//变为待提交状态
                }
                //同意
                else if(wfStatus.intValue()==4){
                    byId.setStatus("10");//变为待调整状态
                }
                //换标流程
                if(byId.getType().equals("1")){

                    //拒绝
                    if(wfStatus.intValue()==3){
                        //解锁库存
                        try{
                            adjustmentOrderService.cancelLockpInventory(byId.getId());
                            adjustmentOrderService.cancelLocksInventory(byId.getId());
                        }catch (Exception e){
                            Assert.isTrue(false,e.getLocalizedMessage());
                        }
                    }
                    //同意
                    else if(wfStatus.intValue()==4){
//                        //锁库存
//                        try{
//                            adjustmentOrderService.lockStockOut(byId.getId());//出库
//                            adjustmentOrderService.stockIn(byId.getId());//入库
//                        }catch (Exception e){
//                            Assert.isTrue(false,e.getLocalizedMessage());
//                        }
                    }

                }
            }
            adjustmentOrderService.updateById(byId);
        }
    }
}
